﻿using System;
using iSun.Model;
using iSun.Web.UI.Controls;
using iSun.BLL.Base;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Linq;

namespace iSun.WebApp.Extend
{
    public partial class eMasterDetail : MasterDetailPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            InitPage2();
        }      /// <summary>



        /// 初始化页面
        /// </summary>
        public void InitPage2()
        {
            string strEntity = this.GetStringFromQueryString("entity", "E_Menu");
            string id = this.GetStringFromQueryString("id", "");
            BaseEntity instance = iSun.Common.Globals.getEntity(strEntity) as BaseEntity;
            BaseBussnies bInstance = new BLL.Base.BaseBussnies(instance);
            BaseEntity entity = bInstance.GetEntityById(id) as BaseEntity;
            
            System.Web.UI.WebControls.Literal li = (this.Controls[0].FindControl("EditTemplate") as System.Web.UI.WebControls.Literal);
            if (li != null)
            {
                li.Text = CreateHtml(instance, entity);
            }


            EditUIConfig ConfigEntity = EditUIConfig.New();
            bInstance.entity = ConfigEntity;
            bInstance.Order = "iOrder";
            string formReForm = this.GetStringFromQueryString("formReForm", "false");

            IList<EditUIConfig> list = bInstance.GetEntitysByCondition<EditUIConfig>("EntityName='" + strEntity + "'");
            int? ColIndex = list.Max(p => p.ColumnIndex);
            StringBuilder strHtml = new StringBuilder();
            if (!string.Equals(formReForm, "false"))
            {
                strHtml.Append(CreateEditReForm(list));
            }
            else
            {
                strHtml.Append("<Table id=\"EditForm\"><tr>");
                for (int i = 0; i < ColIndex + 1; i++)
                {
                    strHtml.Append("<td valign='top' id=\"Col" + i.ToString() + "\"> ");

                    if (entity == null)
                    {
                        strHtml.Append(CreateEditForm(list.Where(p => p.ColumnIndex == i).ToList()));
                    }
                    else
                    {
                        strHtml.Append(CreateEditForm(entity, list.Where(p => p.ColumnIndex == i).ToList()));
                    }
                    strHtml.Append("</td>");
                }
                strHtml.Append("</tr></table>");
            }

            this.FormTemplate.Text = strHtml.ToString();
            li = (this.Controls[0].FindControl("FormTemplate") as System.Web.UI.WebControls.Literal);
            if (li != null)
            {
                li.Text = strHtml.ToString();
            }
            li = (this.Controls[0].FindControl("jSave") as System.Web.UI.WebControls.Literal);
            if (li != null)
            {
                li.Text = CreateScript(list);
            }
            li = (this.Controls[0].FindControl("jInitControl") as System.Web.UI.WebControls.Literal);
            if (li != null)
            {
                li.Text = InitControl(list);
            }
            li = (this.Controls[0].FindControl("MasterID") as System.Web.UI.WebControls.Literal);
            if (li != null)
            {
                li.Text = SetMasterID();
            }

        }

    }
}